/*
     Copyright (C) 2011 Benny Mackney

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "ParticleSystem.h"

ParticleSystem::ParticleSystem()
{
    //ctor
    sprite = NULL;
    width = 1;
    height = 1;
    startColour = {255,255,255,0};
    colourmode = false;
    particleCount = 0;
}

ParticleSystem::~ParticleSystem()
{
    //dtor
    //Free ALL THE MEMORY!

}


void ParticleSystem::add_particle(Sint16 X,Sint16 Y, double XVEL, double YVEL, int AGEDIE){
    particles.push_back(Particle(X,Y,XVEL,YVEL,AGEDIE));
    std::list<Particle>::iterator i;
    i = particles.end();
    i--;
    if(sprite!=NULL){
        i->set_sprite(sprite);
    }
    (*i).set_dimensions(width,height);
    (*i).set_startcolour(&startColour);
    if(colourmode){
        (*i).set_colourmode(true);
        (*i).set_startcolour(&startColour);
        (*i).set_endcolour(&endColour);
    }
    particleCount++;
}

void ParticleSystem::add_particle(Sint16 X,Sint16 Y, double XVEL, double YVEL, double XACC, double YACC, int AGEDIE){
    particles.push_back(Particle(X,Y,XVEL,YVEL,XACC,YACC,AGEDIE));
    std::list<Particle>::iterator i;
    i = particles.end();
    i--;
    if(sprite!=NULL){
        i->set_sprite(sprite);
    }
    (*i).set_dimensions(width,height);
    (*i).set_startcolour(&startColour);
    if(colourmode){
        (*i).set_colourmode(true);
        (*i).set_startcolour(&startColour);
        (*i).set_endcolour(&endColour);
    }
    particleCount++;
}
void ParticleSystem::add_particle(Vec2D POS, Vec2D VEL, int AGEDIE){
    particles.push_back(Particle(POS,VEL,AGEDIE));
    std::list<Particle>::iterator i;
    i = particles.end();
    i--;
    if(sprite!=NULL){
        i->set_sprite(sprite);
    }
    (*i).set_dimensions(width,height);
    (*i).set_startcolour(&startColour);
    if(colourmode){
        (*i).set_colourmode(true);
        (*i).set_startcolour(&startColour);
        (*i).set_endcolour(&endColour);
    }
    particleCount++;
}
void ParticleSystem::add_particle(Vec2D POS, Vec2D VEL, Vec2D ACC, int AGEDIE){
    particles.push_back(Particle(POS,VEL,ACC,AGEDIE));
    std::list<Particle>::iterator i;
    i = particles.end();
    i--;
    if(sprite!=NULL){
        i->set_sprite(sprite);
    }
    (*i).set_dimensions(width,height);
    (*i).set_startcolour(&startColour);
    if(colourmode){
        (*i).set_colourmode(true);
        (*i).set_startcolour(&startColour);
        (*i).set_endcolour(&endColour);
    }
    particleCount++;
}

void ParticleSystem::tick(){
    std::list<Particle>::iterator i;
    i=particles.begin();
    while(i!=particles.end()){
        //Iterate over all particles. tick() them and delete dead ones
        (*i).tick();
        if((*i).dead()){
            i=particles.erase(i);
            particleCount--;

        }else{
            i++;
        }
    }
    return;
}

void ParticleSystem::draw_object(SDL_Surface* screen,SDL_Rect camera){
    //Draw the object onto the screen at (boxRect.x,boxRect.y)
    std::list<Particle>::iterator i;
    for(i=particles.begin();i!=particles.end();i++){
        //Iterate over all particles.
        (*i).draw_object(screen, camera);
    }
}

int ParticleSystem::load_sprite(const char* path){
    if(sprite!=NULL){
        SDL_FreeSurface(sprite);
    }
    sprite = IMG_Load(path);
    if(sprite==NULL){
        return 1;
    }
    std::list<Particle>::iterator i;
    for(i=particles.begin();i!=particles.end();i++){
        (*i).set_sprite(sprite);
    }
    return 0;
}

int ParticleSystem::countParticles(){
    return particleCount;
}

void ParticleSystem::set_width(Uint16 w){
    width = w;
    std::list<Particle>::iterator i;
    for(i=particles.begin();i!=particles.end();i++){
        (*i).set_w(width);
    }

}
void ParticleSystem::set_height(Uint16 h){
    height = h;
    std::list<Particle>::iterator i;
    for(i=particles.begin();i!=particles.end();i++){
        (*i).set_h(h);
    }

}
void ParticleSystem::set_dimensions(Uint16 w,Uint16 h){
    width = w;
    height = h;
    std::list<Particle>::iterator i;
    for(i=particles.begin();i!=particles.end();i++){
        (*i).set_dimensions(w,h);
    }

}

void ParticleSystem::set_startcolour(int r, int g, int b){
    startColour = {b,g,r,0xFF};
}

void ParticleSystem::set_startcolour(SDL_Colour colour){
    startColour = colour;
}
void ParticleSystem::set_endcolour(int r, int g, int b){
    endColour = {b,g,r,0xFF};
}

void ParticleSystem::set_endcolour(SDL_Colour colour){
    endColour = colour;
}

void ParticleSystem::set_colourmode(bool mode){
    colourmode=mode;
}

void ParticleSystem::delete_in_box(const SDL_Rect& delbox){
    std::list<Particle>::iterator i;
    i=particles.begin();
    while(i!=particles.end()){
        if((*i).collides(&delbox)){
            i=particles.erase(i);
            particleCount--;

        }else{
            i++;
        }
    }
}

void ParticleSystem::delete_in_box(const Box& delbox){
    std::list<Particle>::iterator i;
    i=particles.begin();
    while(i!=particles.end()){
        if((*i).collides(&delbox)){
            i=particles.erase(i);
            particleCount--;
        }else{
            i++;
        }
    }
}
